package com.pai.gui.component;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImagingOpException;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.pai.entity.PaiTask;
import com.pai.entity.UserList;
import com.pai.service.SocketThread;

/**
 * 正在传输的任务列表cell布局对象。
 * 
 * @author Jeff+zhang
 * 
 */
public class WorkingTaskCell extends JPanel {

	private static final long serialVersionUID = 1L;

	// private boolean fileSender;
	private int isPause = 0;
	private boolean isCancel = false;
	private boolean isConfirm = false;
	private boolean isClosed = false;
	private boolean isReject = false;
	private boolean isResend = false;
	private String fileName;
	private String fileRealName;
	private String fileNameScr;
	private int index = 0;
	private int state = 0;// 0=pause 1=play.
	private WorkingTaskCell per2CellPanel = this;
	private WorkingTaskList baseList;
	private PaiTask bean;
	private SocketThread st;
	// 界面组件：
	private JLabel iconlabel;
	private JLabel filenamelabel;
	private JLabel fromlabel;
	private JLabel sizelabel;
	private JButton pausebtn;
	private JButton stopbtn;
	private JButton playbtn;
	private JProgressBar progressbar;
	// 界面组件end。

	private int maxwidth = 400;
	private int maxhight = 200;

	public WorkingTaskCell() {
		super();
		init();
	}

	public PaiTask getBean() {
		return bean;
	}

	public void setBean(PaiTask bean) {
		this.bean = bean;
	}

	private void init() {
		this.setSize(new Dimension(maxwidth, maxhight));
		this.setOpaque(false);
		this.setLayout(null);
	}

	public JComponent getListCell(WorkingTaskList list, Object value) {
		System.out.println("WorkingTaskCell-> getListCell");

		this.bean = (PaiTask) value;
		this.baseList = list;
		this.fileName = bean.getFileName();
		// component定义
		iconlabel = new JLabel(new ImageIcon("res/icon/file.png"));

		System.out.println("working task cell : " + bean.getFileName());
		System.out.println("working task cell : " + bean.getFileSize());
		filenamelabel = new JLabel(bean.getFileName());
		filenamelabel.setFont(new java.awt.Font("Arial", 1, 12));
		filenamelabel.setForeground(Color.white);

		fromlabel = new JLabel(/* bean.getState() + " : " + */bean
				.getFromUser().getUsername());
		fromlabel.setFont(new java.awt.Font("Arial", 1, 12));
		fromlabel.setForeground(Color.white);
		String sizetext = "Size:";
		DecimalFormat df = new DecimalFormat("0.00");
		if (bean.getFileSize() > 1024 * 1024L) {
			sizetext += df.format(bean.getFileSize() / 1024.0 / 1024.0) + "MB";
		} else if (bean.getFileSize() > 1024L) {
			sizetext += df.format(bean.getFileSize() / 1024.0) + "KB";
		} else {
			sizetext += df.format(bean.getFileSize()) + "B";
		}
		sizelabel = new JLabel(sizetext);
		sizelabel.setFont(new java.awt.Font("Arial", 1, 12));
		sizelabel.setForeground(Color.white);

		pausebtn = new JButton(new ImageIcon(
				"res/icon/buttonIcon/pausebtn20 20.png"));
		stopbtn = new JButton(new ImageIcon(
				"res/icon/buttonIcon/stopbtn20 20.png"));
		// 新加的开始按钮
		playbtn = new JButton(new ImageIcon(
				"res/icon/buttonIcon/playbtn20 20.png"));

		progressbar = new JProgressBar();

		// 定义：
		iconlabel.setBounds(15, 15, 50, 50);
		filenamelabel.setBounds(80, 16, 150, 12);
		fromlabel.setBounds(80, 34, 80, 12);
		sizelabel.setBounds(200, 34, 140, 12);

		pausebtn.setBounds(345, 30, 20, 20);
		pausebtn.setBorder(null);
		pausebtn.setOpaque(false);
		pausebtn.setContentAreaFilled(false);

		stopbtn.setBounds(365, 30, 20, 20);
		stopbtn.setBorder(null);
		stopbtn.setOpaque(false);
		stopbtn.setContentAreaFilled(false);

		playbtn.setBounds(325, 30, 20, 20);
		playbtn.setBorder(null);
		playbtn.setOpaque(false);
		playbtn.setContentAreaFilled(false);
		playbtn.setVisible(false);
		progressbar.setBounds(78, 54, 307, 10);
		progressbar.setMinimum(0);
		progressbar.setMaximum(100);
		progressbar.setValue(0);// 初始时为0
		progressbar.setStringPainted(true);
		progressbar.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				// 备用方法。
			}
		});

		pausebtn.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("mouse click pause");
				playbtn.setVisible(true);
				pausebtn.setVisible(false);
				isPause++;
				isCancel = true;
				isResend = false;

				if (isPause == 1) // 暂停
					isPause = 0;
			}
		});
		pausebtn.setRolloverIcon(new ImageIcon(
				"res/icon/buttonIcon/pausebtn2.png"));// 经过
		pausebtn.setPressedIcon(new ImageIcon(
				"res/icon/buttonIcon/pausebtn3.png"));// 按下

		stopbtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("mouse click stop");
				isClosed = true;
				isCancel = true;
			}
		});
		stopbtn.setRolloverIcon(new ImageIcon(
				"res/icon/buttonIcon/stopbtn2.png"));// 经过
		stopbtn.setPressedIcon(new ImageIcon("res/icon/buttonIcon/stopbtn3.png"));// 按下

		// ///////////请重写这里的播放键的动作
		playbtn.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("play mouse click pause");
				isResend = true;
				isCancel = false;
				pausebtn.setVisible(true);
				playbtn.setVisible(false);
				/*
				 * isPause++; if(isPause == 1) //暂停 isPause = 0;
				 */
			}
		});
		playbtn.setRolloverIcon(new ImageIcon(
				"res/icon/buttonIcon/playbtn2.png"));// 经过
		playbtn.setPressedIcon(new ImageIcon("res/icon/buttonIcon/playbtn3.png"));// 按下

		this.add(iconlabel);
		this.add(filenamelabel);
		this.add(fromlabel);
		this.add(sizelabel);
		this.add(pausebtn);
		this.add(stopbtn);
		this.add(playbtn);
		this.add(progressbar);
		this.setOpaque(false);
		this.setMaximumSize(new Dimension(800, 80));

		return this;
	}

	public int getIsPause() {
		return isPause;
	}

	/**
	 * 调用此方法来改变当前cell的progressbar的值
	 * 
	 * @param isPause
	 */
	public void changeProgressbarValue(int value) {
		progressbar.setValue(value);
	}

	public void setIsPause(int isPause) {
		this.isPause = isPause;
	}

	public void setSelect(boolean iss) {

	}

	public boolean isCancel() {
		return isCancel;
	}

	public void setCancel(boolean isCancel) {
		this.isCancel = isCancel;
	}

	public boolean isReject() {
		return isReject;
	}

	public void setReject(boolean isReject) {
		this.isReject = isReject;
	}

	public void setMaxwidth(int maxwidth) {
		this.maxwidth = maxwidth;
	}

	public void setMaxhight(int maxhight) {
		this.maxhight = maxhight;
	}

	public boolean isClosed() {
		return isClosed;
	}

	public void setClosed(boolean isClosed) {
		this.isClosed = isClosed;
	}

	public boolean isResend() {
		return isResend;
	}

	public void setResend(boolean isResend) {
		this.isResend = isResend;
	}

	/**
	 * 弹出对话框方法，用户点击对话框的确定按钮后会选择接收文件。
	 */
	public void JFPointOut() {
		JFileChooser filechooser = new JFileChooser();

		File file;
		// String filename;
		Object[] options = { "接收", "拒绝" };
		int m = JOptionPane.showOptionDialog(
				this,
				UserList.getUserByIp(st.getIP()).getUsername() + "发来文件:"
						+ st.getFileName(), "收到文件", JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		if (m == 0) {
			int val = filechooser.showSaveDialog(this);// 这个地方换成父panel。
			if (val == JFileChooser.APPROVE_OPTION) {
				file = filechooser.getSelectedFile();
				fileName = file.getAbsolutePath();
				fileNameScr = file.getAbsolutePath();
				fileRealName = file.getName();
				System.out.println(fileName);
				// 操作
				isConfirm = true;
			} else {
				System.out.println("cancle");
				// 拒绝的代码
				// isCancel = true;
			}
		} else {
			// 拒绝接收的代码
			isCancel = true;
			isReject = true;
			// isClosed = true;
		}
	}

	public String getFileName() {
		return fileName;
	}

	public String getFileNameScr() {
		return fileNameScr;
	}

	public void setFileNameScr(String fileNameScr) {
		this.fileNameScr = fileNameScr;
	}

	public boolean isConfirm() {
		return isConfirm;
	}

	public void setConfirm(boolean isConfirm) {
		this.isConfirm = isConfirm;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}

	public SocketThread getSt() {
		return st;
	}

	public void setSt(SocketThread st) {
		this.st = st;
	}

	public void setPlayBtnVisiable(boolean visiable) {
		playbtn.setVisible(visiable);
		pausebtn.setVisible(!visiable);
	}

}
